<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>精彩回顾</span>
        <el-button
          style="float: right; padding: 0;margin-left:14px"
          type="text"
          @click="goto('/jobfair/list')"
        >
          返回
        </el-button>
      </div>
      <el-upload
        :action="apiUpload"
        :headers="headers"
        list-type="picture-card"
        :on-success="handlePhotoSuccess"
        :before-upload="beforePhotoUpload"
        :on-preview="handlePictureCardPreview"
        :on-remove="handleRemove"
        :file-list="img_src_arr"
      >
        <i class="el-icon-plus" />
      </el-upload>
      <el-row style="margin-top:20px">
        <el-button type="primary" @click="goto('/jobfair/list')">确定</el-button>
        <el-button @click="goto('/jobfair/list')">返回</el-button>
      </el-row>
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { retrospect, retrospectDelete, retrospectAdd } from '@/api/jobfair'
import apiArr from '@/api'
import { getToken } from '@/utils/auth'

export default {
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      headers: { admintoken: getToken() },
      fileupload_size: '',
      fileupload_ext: '',
      apiUpload: window.global.RequestBaseUrl + apiArr.upload,
      img_src_arr: []
    }
  },
  computed: {
    config() {
      return this.$store.state.config
    }
  },
  created() {
    this.fileupload_size = this.config.fileupload_size
    this.fileupload_ext = this.config.fileupload_ext
    this.fetchInfo()
  },
  methods: {
    fetchInfo() {
      const param = {
        jobfair_id: this.$route.query.id
      }
      retrospect(param, 'get').then(response => {
        for (var i in response.data.items){
          this.img_src_arr.push({
            name: response.data.items[i].id,
            url: response.data.items[i].img
          })
        }
      })
    },
    handleRemove(file, fileList) {
      const param = {
        id: file.name,
        jobfair_id: this.$route.query.id
      }
      retrospectDelete(param).then(response => {
        this.$message.success(response.message)
        return true
      })
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handlePhotoSuccess(res, file) {
      if (res.code == 200) {
        const param = {
          jobfair_id: this.$route.query.id,
          img: res.data.file_id
        }
        retrospectAdd(param).then(response => {
          file.name = response.data
          this.$message.success(response.message)
          return true
        })
      } else {
        this.$message.error(res.message)
        return false
      }
    },
    beforePhotoUpload(file) {
      const filetypeArr = file.type.split('/')
      const filetype = filetypeArr[1]
      const configFileExtArr = this.fileupload_ext.split(',')

      if (!configFileExtArr.includes(filetype)) {
        this.$message.error('上传文件格式不允许')
        return false
      }
      if (file.size / 1024 > this.fileupload_size) {
        this.$message.error(`上传文件最大为${this.fileupload_size}kb`)
        return false
      }
      return true
    },
    goto(target) {
      this.$router.push(target)
    }
  }
}
</script>
<style lang="scss" scoped>
  .break{padding-top:20px}
</style>
